<style scoped lang="scss">
@import url('index.scss');
</style>

<template>
  <div class="app-container">
    <el-row>
      <el-col :span="leftspan">
        <div class="left_con">
          <div class="toptime">
            <!-- <el-date-picker-panel v-model="indexdate" /> -->
            <el-date-picker-panel type="date" v-model="indexdate" :border="false" @panel-change="handlePanelChange"
              date-format="YYYY-MM-DD" :disabled-date="disabledDate">
              <!-- <template #default="{ date, isSelected }">
                <CalendarCell :date="date" :is-selected="isSelected" :weekend-class="weekendClass(date)" />
              </template> -->
            </el-date-picker-panel>
          </div>
          <div class="searchform">
            <div class="searchform_item">
              <el-form :model="queryParamsPatient" ref="queryRef" :inline="true" v-show="showSearch"
                label-width="100px">
                <el-form-item label="姓名" prop="uname">
                  <el-input v-model="queryParamsPatient.uname" placeholder="请输入患者姓名" clearable
                    @keyup.enter="handleQueryPatient" />
                </el-form-item>
                <el-form-item label="身份证号" prop="ucard">
                  <el-input v-model="queryParamsPatient.ucard" placeholder="请输入患者身份证号" clearable
                    @keyup.enter="handleQueryPatient" />
                </el-form-item>
                <el-form-item label="手机号" prop="umobile">
                  <el-input v-model="queryParamsPatient.umobile" placeholder="请输入患者手机号" clearable
                    @keyup.enter="handleQueryPatient" />
                </el-form-item>
                <!-- <el-form-item label="出生年月" prop="ubirthday">
                  <el-input v-model="queryParams.ubirthday" placeholder="请输入患者出生年月" clearable
                    @keyup.enter="handleQuery" />
                </el-form-item> -->
                <el-form-item label="社保卡号" prop="ssid">
                  <el-input v-model="queryParamsPatient.ssid" placeholder="请输入患者社保卡号" clearable
                    @keyup.enter="handleQueryPatient" />
                </el-form-item>
                <el-form-item label="预约检查项" prop="appointmenttime">
                  <!-- <el-date-picker clearable v-model="queryParams.appointmenttime" type="date" value-format="YYYY-MM-DD"
                    placeholder="请选择预约时间">
                  </el-date-picker> -->
                  <el-input v-model="queryParams.ssid" placeholder="请输入检查项" clearable
                    @keyup.enter="handleQueryPatient" />
                </el-form-item>
                <el-form-item>
                  <el-button class="newButton" icon="Search" @click="handleQueryPatient">查询</el-button>
                  <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </el-col>
      <el-col :span="0.5" class="newCenter" :class="{'radius':currentShow === 'left'}">
        <div class="showLine">
          <img src="@/assets/images/left.svg" alt="" class="left" @click="handleLeftClick"
            v-if="currentShow === 'left'">
          <img src="@/assets/images/left.svg" alt="" class="right" @click="handleRightClick"
            v-if="currentShow === 'right'">
        </div>
      </el-col>
      <el-col :span="rightspan"
        :style="{'min-width': currentShow === 'right' ? 'calc(100% - 20px)!important':'calc(79% - 20px)!important'}"
        style="padding-left: 16px;">

        <!---右侧头部-->

        <div class="newTop">
          <div class="newtable">
            <el-table v-loading="loadingPatient" :data="patientList" @selection-change="handleSelectionChangePatient"
              :cell-style="{ backgroundColor: '#f9f9f9' }">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column type="expand" width="55" label="展开">
                <template #default="props">
                  <div class="detail">
                    <el-row :gutter="20">
                      <el-col :span="8">
                        <p><b>姓名：</b>{{ props.row.uname }}</p>
                      </el-col>
                      <el-col :span="8">
                        <p><b>性别：</b>{{ props.row.usex == 0 ? '男' : (props.row.usex == 1 ? '女' : '未知') }}</p>
                      </el-col>
                      <el-col :span="8">
                        <p><b>年龄：</b>{{ props.row.age }}</p>
                      </el-col>
                    </el-row>
                    <el-row :gutter="20">
                      <el-col :span="8">
                        <p><b>身份证号：</b>{{ props.row.ucard }}</p>
                      </el-col>
                      <el-col :span="8">
                        <p><b>手机号：</b>{{ props.row.umobile }}</p>
                      </el-col>
                      <el-col :span="8">
                        <p><b>出生年月：</b>{{ props.row.ubirthday }}</p>
                      </el-col>
                    </el-row>
                    <el-row :gutter="20">
                      <el-col :span="8">
                        <p><b>社保卡号：</b>{{ props.row.ssid }}</p>
                      </el-col>
                      <el-col :span="8">
                        <p><b>婚姻情况：</b>{{ props.row.maritalstatus }}</p>
                      </el-col>
                      <el-col :span="8">
                        <p><b>居住地址：</b>{{ JSON.parse(props.row.addresscity).map(item => item).join('') +
                          props.row.address }}</p>
                      </el-col>
                    </el-row>
                    <el-row :gutter="20">
                      <!-- <el-col :span="8">
                    <p><b>预约类型：</b>{{ props.row.appointmenttype }}</p>
                  </el-col> -->
                      <!-- <el-col :span="8" v-if="props.row.appointmenttime">
                    <p><b>预约时间：</b>{{ parseTime(props.row.appointmenttime, '{y}-{m}-{d}') }}</p>
                  </el-col>
                  <el-col :span="16" v-if="!props.row.appointmentitems">
                    <p><b>预约项目：</b>{{ JSON.parse(props.row.appointmentitems).map(item => checkList.find(listItem => listItem.itemid == item)?.name).join('、') }}</p>
                  </el-col> -->
                    </el-row>
                    <!-- <el-row :gutter="20">
                  <el-col :span="24">
                    <p><b>预约项目：</b>{{ JSON.parse(props.row.appointmentitems).map(item => checkList.find(listItem => listItem.itemid == item)?.name).join('、') }}</p>
                  </el-col>
                </el-row> -->
                  </div>
                </template>
              </el-table-column>

              <el-table-column label="患者ID" width="65" align="center" prop="uid">
              </el-table-column>
              <el-table-column label="姓名" min-width="85" align="center" prop="uname" />
              <el-table-column label="性别" width="55" align="center" prop="usex">
                <template #default="scope">
                  <span v-if="scope.row.usex == '0'">男</span>
                  <span v-else-if="scope.row.usex == '1'">女</span>
                  <span v-else>未知</span>
                </template>
              </el-table-column>
              <el-table-column label="年龄" width="55" align="center" prop="age" />
              <el-table-column label="婚姻状态" width="85" align="center" prop="wedstatus">
                <template #default="scope">
                  {{ ha_wed_status.find(item => item.value == scope.row.maritalstatus)?.label || '未知' }}
                </template>
              </el-table-column>
              <el-table-column label="身份证号" align="center" prop="ucard" />
              <el-table-column label="手机号" align="center" prop="umobile" />
              <el-table-column label="出生年月" align="center" prop="ubirthday" />
              <!-- <el-table-column label="社保卡号" width="180"  align="center" prop="ssid" /> -->
              <!-- <el-table-column label="婚姻情况" align="center" prop="maritalstatus" />-->
              <el-table-column label="居住地址" align="center" prop="address">
                <template #default="scope">
                  <span>{{ JSON.parse(scope.row.addresscity).map(item => item).join('') + scope.row.address }}</span>
                </template>
              </el-table-column>
              <!-- <el-table-column label="预约类型" align="center" prop="appointmenttype" /> -->
              <!-- <el-table-column label="预约项目"  align="center" prop="appointmentitems" >
              <template #default="scope">
                <span>{{ JSON.parse(scope.row.appointmentitems).map(item => checkList.find(listItem => listItem.itemid == item)?.name).join('、') }}</span>
              </template>
            </el-table-column> -->
              <!-- <el-table-column label="预约时间" align="center" prop="appointmenttime" width="180">
              <template #default="scope">
                <span>{{ parseTime(scope.row.appointmenttime, '{y}-{m}-{d} {h}:{i}') }}</span>
              </template>
            </el-table-column> -->
              <!-- <el-table-column label="发送短信" align="center" prop="sendsms" /> -->
              <!-- <el-table-column label="预约状态" align="center" prop="ustatus" >
              <template #default="scope">
                <el-tag v-if="scope.row.ustatus == '0'" type="error">未预约</el-tag>
                <el-tag v-else-if="scope.row.ustatus == '1'" type="success">已预约</el-tag>
                <el-tag v-else type="error">未预约</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="就诊状态" align="center" prop="pstatus" >
              <template #default="scope">
                <el-tag v-if="scope.row.pstatus == '0'" type="error">未就诊</el-tag>
                <el-tag v-else-if="scope.row.pstatus == '1'" type="info">就诊中</el-tag>
                <el-tag v-else-if="scope.row.pstatus == '2'" type="success">已就诊</el-tag>
                <el-tag v-else type="error">未就诊</el-tag>
              </template>
            </el-table-column> -->
              <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
              <!-- <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
              <template #default="scope">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:patient:edit']">修改</el-button>
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['system:patient:remove']">删除</el-button>
              </template>
            </el-table-column> -->
            </el-table>
            <pagination v-show="patientTotal > 0" :total="patientTotal" v-model:page="queryParamsPatient.pageNum"
              v-model:limit="queryParamsPatient.pageSize" @pagination="getPatientList" />
          </div>
        </div>

        <div class="right_top topsearch">
          <el-row>
            <el-col :span="24" class="rightcol">
              <div class="newbtn" @click="handleAdd" v-hasPermi="['system:appiontment:add']">
                <img src="@/assets/images/add.svg" alt="">
                <span>预约</span>
              </div>
              <div class="newbtn" v-if="single == false" @click="handleUpdate" v-hasPermi="['system:appiontment:edit']">
                <img src="@/assets/images/edit.svg" alt=""></img>
                <span>编辑</span>
              </div>
              <div class="newbtn" v-if="multiple == false" @click="handleDelete"
                v-hasPermi="['system:appiontment:remove']">
                <img src="@/assets/images/del.svg" alt=""></img>
                <span>删除</span>
              </div>
              <div class="newbtn" @click="handleExport" v-hasPermi="['system:appiontment:export']">
                <img src="@/assets/images/export.svg" alt=""></img>
                <span>导出</span>
              </div>
              <div class="newbtn" @click="getList">
                <img src="@/assets/images/refresh.svg" alt=""></img>
                <span>刷新</span>
              </div>
            </el-col>
          </el-row>
        </div>


        <!-- <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="患者ID" prop="pid">
            <el-input v-model="queryParams.pid" placeholder="请输入患者ID" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="预约科室" prop="kid">
            <el-input v-model="queryParams.kid" placeholder="请输入预约科室" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="预约检查项目" prop="appointmentitems">
            <el-input v-model="queryParams.appointmentitems" placeholder="请输入预约检查项目" clearable
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="预约时间" prop="appointmenttime">
            <el-date-picker clearable v-model="queryParams.appointmenttime" type="date" value-format="YYYY-MM-DD"
              placeholder="请选择预约时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form> -->

        <!-- <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd"
              v-hasPermi="['system:appiontment:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
              v-hasPermi="['system:appiontment:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['system:appiontment:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport"
              v-hasPermi="['system:appiontment:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

        <div class="newtable">
          <el-table v-loading="loading" :data="appiontmentList" @selection-change="handleSelectionChange"
            :cell-style="{ backgroundColor: '#f9f9f9' }">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="ID" width="55" align="center" prop="aid" />
            <el-table-column label="患者ID" width="85" align="center" prop="pid" />
            <el-table-column label="患者姓名" min-width="85" align="center" prop="uname" />
            <el-table-column label="预约科室" width="95" align="center" prop="kid">
              <template #default="scope">
                {{departmentList.find(item => item.kid == scope.row.kid)?.kname || '-'}}
              </template>
            </el-table-column>
            <!-- <el-table-column label="预约类型" align="center" prop="appointmenttype" /> -->
            <el-table-column label="预约检查项目" align="center" prop="appointmentitems">
              <template #default="scope">
                {{ scope.row.appointmentitems ? JSON.parse(scope.row.appointmentitems).map(item =>
                checkList.find(listItem => listItem.itemid== item)?.name).join('、') : '-' }}
                <!-- <span>{{JSON.parse(scope.row.appointmentitems).map(item => checkList.find(listItem => listItem.itemid== item)?.name).join('、') }}</span> -->
              </template>
            </el-table-column>
            <el-table-column label="预约时间" align="center" prop="appointmenttime" width="180">
              <template #default="scope">
                <span>{{ parseTime(scope.row.appointmenttime, '{y}-{m}-{d} {h}:{i}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="预约状态" align="center" prop="ustatus">
              <template #default="scope">
                <el-tag v-if="scope.row.ustatus == 1" type="success">已预约</el-tag>
                <el-tag v-else-if="scope.row.ustatus == 0" type="danger">未预约</el-tag>
                <el-tag v-else type="info">未知状态</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="就诊状态" align="center" prop="pstatus" >
              <template #default="scope">
                <el-tag v-if="scope.row.pstatus == 1" type="success">已就诊</el-tag>
                <el-tag v-else-if="scope.row.pstatus == 0" type="danger">未就诊</el-tag>
                <el-tag v-else type="info">未知状态</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="预约备注" align="center" prop="remark" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template #default="scope">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:appiontment:edit']">修改</el-button>
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['system:appiontment:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
        </div>


      </el-col>
    </el-row>


    <!-- 添加或修改预约列表对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" class="newDialog">
      <el-form ref="appiontmentRef" :model="form" :rules="rules" label-width="100px">

        <el-row :gutter="20">
          <el-col :span="8">
            <div class="patientInfo">
              <el-form-item label="患者ID" prop="pid" v-if="singlePatient==false">
                <el-input v-model="form.pid" placeholder="请输入患者ID" disabled />
              </el-form-item>
              <el-form-item label="患者ID" prop="pid" v-if="singlePatient==true">
                <el-select v-model="form.pid" filterable remote reserve-keyword clearable placeholder="请查找选择患者"
                  :remote-method="remoteMethod" style="width: 100%" @change="handleChangePatient">
                  <el-option v-for="item in options" :key="item.uid" :label="item.uname" :value="item.uid" />
                </el-select>
              </el-form-item>
              <el-form-item label="姓名" prop="uname">
                <el-input v-model="uinfo.uname" placeholder="请输入姓名" disabled />
              </el-form-item>
              <el-form-item label="性别" prop="usex">
                <el-select v-model="uinfo.usex" placeholder="请选择性别" clearable disabled>
                  <el-option v-for="item in sys_user_sex" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
              <el-form-item label="身份证号" prop="ucard">
                <el-input v-model="uinfo.ucard" placeholder="请输入身份证号" disabled />
              </el-form-item>
              <el-form-item label="出生年月" prop="ubirthday">
                <!-- <el-input v-model="form.ubirthday" placeholder="请输入患者出生年月" /> -->
                <el-date-picker clearable v-model="uinfo.ubirthday" type="date" value-format="YYYY-MM-DD" disabled
                  placeholder="请选择出生年月" style="width: 100%;">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="社保卡号" prop="ssid">
                <el-input v-model="uinfo.ssid" placeholder="请输入社保卡号" disabled />
              </el-form-item>
              <el-form-item label="手机号" prop="umobile">
                <el-input v-model="uinfo.umobile" placeholder="请输入手机号" disabled />
              </el-form-item>
              <el-form-item label="婚姻状态" prop="wedstatus">
                <el-select v-model="uinfo.maritalstatus" placeholder="请选择婚姻状态" clearable disabled>
                  <el-option v-for="item in ha_wed_status" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
              <el-form-item label="居住地址" prop="address" v-if="uinfo.addresscity">
                <!-- {{ JSON.parse(uinfo.addresscity).map(item => item).join('') + uinfo.row.address }} -->
                {{ JSON.parse(uinfo.addresscity) + uinfo.address }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="16">
            <el-form-item label="预约科室" prop="kid">
              <!-- <el-input v-model="form.kid" placeholder="请输入预约科室" /> -->
              <el-select v-model="form.kid" placeholder="请选择科室" clearable>
                <el-option v-for="item in departmentList" :key="item.kid" :label="item.kname" :value="item.kid" />
              </el-select>
            </el-form-item>
            <el-form-item label="预约检查项" prop="appointmentitems">
              <!-- <el-input v-model="form.appointmentitems" placeholder="请输入预约检查项目" /> -->
              <el-select v-model="form.appointmentitems" multiple clearable placeholder="请选择预约检查项目"
                style="width: 100%;">
                <el-option v-for="item in checkList" :value="item.itemid" :key="item.itemid" :label="item.name">{{
                  item.name }}</el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="预约时间" prop="appointmenttime">
              <el-date-picker clearable v-model="form.appointmenttime" type="datetime" format="YYYY-MM-DD HH:mm"
                value-format="YYYY-MM-DD HH:mm" placeholder="请选择预约时间" style="width: 100%;">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="预约备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入预约备注" type="textarea" :rows="6" />
            </el-form-item>
            <el-form-item label="预约状态" prop="ustatus">
              <!-- <el-select v-model="form.ustatus" placeholder="请选择预约状态" clearable>
                <el-option v-for="item in ha_appoint_status" :key="item.value" :label="item.label" :value="item.value" />
              </el-select> -->
              <el-radio-group v-model="form.ustatus" style="display: inline-block;">
                <el-radio v-for="item in ha_appoint_status" :key="item.value" :label="item.value" :value="item.value">{{
                  item.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Appiontment">
import { listAppiontment, getAppiontment, delAppiontment, addAppiontment, updateAppiontment } from "@/api/system/appiontment"
import { listPatient, getPatient, delPatient, addPatient, updatePatient } from "@/api/system/patient"
import { listCheckitems } from "@/api/system/checkitems"
import { listDepartment } from "@/api/system/department"

import dayjs from 'dayjs';
import { onMounted } from "vue";

const { proxy } = getCurrentInstance()
//性别字典
const { sys_user_sex } = proxy.useDict('sys_user_sex')
const { ha_wed_status } = proxy.useDict('ha_wed_status') //婚姻状态
const { ha_appoint_status } = proxy.useDict('ha_appoint_status') //预约状态

const patientList = ref([]) // 获取医生列表
const loadingPatient = ref(true) // 患者列表加载状态
const patientTotal = ref(0) // 患者列表总条数
const options = ref([]) // 患者列表选项,查找的用户
const indexdate = ref(dayjs().format('YYYY-MM-DD')) 

const appiontmentList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
//患者的单选
const idsPatient = ref([])
const singlePatient = ref(true)
const multiplePatient = ref(true)
const uinfo = ref({})
//
const total = ref(0)
const title = ref("")
const currentShow = ref('left')
const leftspan = ref(5)
const rightspan = ref(18)
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 5,
    pid: null,
    kid: null,
    appointmenttype: null,
    appointmentitems: null,
    appointmenttime: null,
    ustatus: null,
    pstatus: null,
  },
  queryParamsPatient: {
    pageNum: 1,
    pageSize: 5,
    pid: null,
    kid: null,
    uname: null,
    usex: null,
    ucard: null,
    umobile: null,
    ubirthday: null,
    ssid: null,
  },
  rules: {
    pid: [{ required: true, message: '请选择患者', trigger: 'change' }],
    kid: [{ required: true, message: '请选择科室', trigger: 'change' }],
    appointmentitems: [{ required: true, message: '请选择预约检查项目', trigger: 'change' }],
    appointmenttime: [{ required: true, message: '请选择预约时间', trigger: 'change' }],
    ustatus: [{ required: true, message: '请选择预约状态', trigger: 'change' }],
  }
})

const { queryParams, form, rules ,queryParamsPatient} = toRefs(data)
function handlePanelChange(date) { //面板切换，有效果
  console.log(date)
}
const propsNew = ref({
  value: 'name',  // 指定 value 字段为 'code'  //直接用实际地址拼接起来
  label: 'name',  // 指定 label 字段为 'name'
  children: 'children', // 指定子节点字段为 'children'
});
const checkList = ref([
  // '甲状腺功能测定',
  // '骨代谢功能显像',
  // '心肌灌注显像',
  // '肾动态显像',
  // '肿瘤代谢显像（PET/CT）',
])
  const newqueryParams = {
    pageNum: 1,
    pageSize: 1000,
  }
  // 获取科室列表
const departmentList = ref([])


onMounted(async () => {
  //查询所有检查项列表
  const checkitemsResponse = await listCheckitems(newqueryParams)
  if (checkitemsResponse.code == 200) {
    checkList.value = checkitemsResponse.rows.map(item => ({
      name: item.itemname,
      itemid: item.itemid,
    })) //名称和itemid形成新数组
    
  } else{
    checkList.value = []
  }
  //列出所有的科室
getDepartmentList()
  //end

})

// 获取科室列表@鲍小伟20251111
function getDepartmentList() {
  listDepartment(newqueryParams).then(response => {
    console.log(response)
    if(response.code == 200){
      departmentList.value = response.rows
    } else {
      departmentList.value = []
    }
  })
}

//选择框下拉
function handleChangePatient() {
  console.log(form.value.pid)
  //查询患者的信息
  getPatientInfo(form.value.pid)
}
//查询患者的信息
function getPatientInfo(pid) {
  getPatient(pid).then(response => {
     console.log(response)
     //将患者的信息赋值给表单
     uinfo.value = response.data
  })
}
//查找的患者列表
function remoteMethod(queryString, callback) {
  console.log(queryString)
  queryParamsPatient.value.uname = queryString
  queryParamsPatient.value.pageSize = 1000
   listPatient(queryParamsPatient.value).then(response => {
    options.value = response.rows
  })
}
//获取患者列表
function getPatientList() {
  loadingPatient.value = true
  listPatient(queryParamsPatient.value).then(response => {
        // 计算年龄鲍小伟
    response.rows.forEach(item => {
      item.age = proxy.$filters.age(item.ubirthday)
    })
    patientList.value = response.rows
    patientTotal.value = response.total
    loadingPatient.value = false
  })
}
//不可用日期
function disabledDate(time) {
  // time 是候选日期对应的 Date 对象
  return time.getTime() > Date.now()   // 今天 00:00:00 以后全部禁用
}


// 切换显示
function handleLeftClick() {
  currentShow.value = 'right'
  leftspan.value = 0
  rightspan.value = 23
}
function handleRightClick() {
  currentShow.value = 'left'
  leftspan.value = 5
  rightspan.value = 18
}

/** 查询预约列表列表 */
function getList() {
  loading.value = true
  listAppiontment(queryParams.value).then(response => {
    appiontmentList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    aid: null,
    pid: null,
    kid: null,
    appointmenttype: null,
    appointmentitems: null,
    appointmenttime: null,
    ustatus: '1',
    pstatus: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  uinfo.value = {}
  proxy.resetForm("appiontmentRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function handleQueryPatient() {
  queryParamsPatient.value.pageNum = 1
  getPatientList()
}


/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
    // console.log(selection)
  ids.value = selection.map(item => item.aid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

// 患者列表选中数据
function handleSelectionChangePatient(selection) {
  // console.log(selection)
  // 患者的多选
  idsPatient.value = selection.map(item => item.uid)
  // 患者的单选
  singlePatient.value = selection.length != 1
  // 患者的多选
  multiplePatient.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset()
  open.value = true
  title.value = "添加预约列表"
  //看一下是否有选择患者
  // 如果没有选择患者，提示用户选择患者
  if (!singlePatient.value) {
    //显示选择的患者id,查询患者的信息
     const _aid = row.uid || idsPatient.value //要和选择的对应，注意
     form.value.pid = _aid //赋值过去
     //查询患者的信息
     getPatientInfo(_aid)
  } else{
    // proxy.$modal.msgError("请选择患者")
    return
  }

}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _aid = row.aid || ids.value
  getAppiontment(_aid).then(response => {
    form.value = response.data
        //处理格式
    form.value.appointmentitems = form.value.appointmentitems?JSON.parse(form.value.appointmentitems):[]
    open.value = true
    title.value = "修改预约列表"
  })
  //获取一下用户信息
   getPatientInfo(row.pid)
  //
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["appiontmentRef"].validate(valid => {
    if (valid) {
      if (form.value.aid != null) {
        //处理一下格式
        form.value.appointmentitems = JSON.stringify(form.value.appointmentitems)
        updateAppiontment(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        //处理一下格式
        form.value.appointmentitems = JSON.stringify(form.value.appointmentitems)
        addAppiontment(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _aids = row.aid || ids.value
  proxy.$modal.confirm('是否确认删除预约列表编号为"' + _aids + '"的数据项？').then(function() {
    return delAppiontment(_aids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/appiontment/export', {
    ...queryParams.value
  }, `appiontment_${new Date().getTime()}.xlsx`)
}

getList()
getPatientList()
</script>
